package com.trans.utils;

import com.trans.anno.Translator;
import com.trans.auxiliary.HandleFullRecord;
import com.trans.auxiliary.Params;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author xwduo
 * @Description //TODO
 * @Date 2023-06-06 09:21
 */
public class ModelUtils {

    private transient static final Map<Class, Map<String, HandleFullRecord>> CACHE = new HashMap<>();

    /**
     * 用于提取ModelJson的注解信息
     * model
     * 模板对象用于提取Map,或者无法在原属性上进行打标记的属性来做翻译
     */
    public static Map<String, HandleFullRecord> extractModelJson(Class model) {
        if (CACHE.containsKey(model)) {
            return CACHE.get(model);
        }
        Field[] fields = model.getDeclaredFields();
        Map<String, HandleFullRecord> modelHandleMap = new HashMap<>();
        for (Field field : fields) {
            Params params = new Params(field);
            Translator customFormat = field.getAnnotation(Translator.class);
            if (null != customFormat) {
                HandleFullRecord handleRecord = new HandleFullRecord(customFormat, params, null, field,null);
                modelHandleMap.put(field.getName(), handleRecord);
            }
        }
        CACHE.put(model, modelHandleMap);
        return modelHandleMap;
    }
}
